using Photon;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class PlayerShooter : Photon.MonoBehaviour
{
	public Weapon[] m_Weapon;

	public PlayerSetup s_PlayerSetup;

	private Transform m_camera;

	public GameObject m_impactPrefab;

	public int m_CurrentWP;

	private GameObject[] m_impacts;

	private int m_currentImpact;

	private int m_maxImpacts = 100;

	private float timerShot;

	private PhotonView MainView;

	public TouchPad m_MaintouchPad;

	public TouchPad m_FireTouchPad;

	public AudioSource audios;

	private Animator anim;

	public ParticleSystem[] ShotFX;

	private void Start()
	{
		m_camera = s_PlayerSetup.s_RigidibodyFPSController.cam.transform;
		anim = s_PlayerSetup.anim;
		for (int i = 0; i < m_Weapon.Length; i++)
		{
			m_Weapon[i].boletsСurent = m_Weapon[i].boletsMax;
		}
		if (PhotonNetwork.room == null || PhotonNetwork.offlineMode || s_PlayerSetup.photonView.isMine)
		{
			m_impacts = new GameObject[m_maxImpacts];
			for (int j = 0; j < m_maxImpacts; j++)
			{
				m_impacts[j] = Object.Instantiate(m_impactPrefab);
				m_impacts[j].transform.SetParent(base.gameObject.transform);
			}
			MainView = GetComponentInParent<PhotonView>();
		}
	}

	private void Update()
	{
		timerShot += Time.deltaTime;
		if (s_PlayerSetup.photonView.isMine)
		{
		}
	}

	public void UpdatePlayerShooter()
	{
		if (s_PlayerSetup.s_PlayerOthers.c_CanvasPlayer.ToolsMenuCanvas.activeSelf)
		{
			anim.SetBool("Shoot_b", false);
			return;
		}
		if (CrossPlatformInputManager.GetButton("Fire"))
		{
			m_MaintouchPad.enabled = false;
			if (!s_PlayerSetup.AWPZoom)
			{
				s_PlayerSetup.s_RigidibodyFPSController.mouseLook.XSensitivity = 0.5f;
				s_PlayerSetup.s_RigidibodyFPSController.mouseLook.YSensitivity = 0.5f;
			}
			m_FireTouchPad.enabled = true;
            anim.SetBool("Shoot_b", true);
        }
		if (CrossPlatformInputManager.GetButtonUp("Fire"))
		{
			m_FireTouchPad.enabled = false;
			if (!s_PlayerSetup.AWPZoom)
			{
				s_PlayerSetup.s_RigidibodyFPSController.mouseLook.XSensitivity = s_PlayerSetup.currentSensivity;
				s_PlayerSetup.s_RigidibodyFPSController.mouseLook.YSensitivity = s_PlayerSetup.currentSensivity;
			}
			m_MaintouchPad.enabled = true;
			anim.SetBool("Shoot_b", false);
		}
		if (CrossPlatformInputManager.GetButtonDown("Reload") && m_Weapon[m_CurrentWP].boletsСurent < m_Weapon[m_CurrentWP].boletsMax)
		{
			anim.SetBool("Shoot_b", false);
			anim.SetBool("Reload_b", true);
		}
	}

	public void Fire()
	{
		if (s_PlayerSetup.photonView.isMine || SavesAllFunctions.Local)
		{
			if (m_Weapon[m_CurrentWP].boletsСurent <= 0)
			{
				anim.SetBool("Reload_b", true);
			}
			else
			{
				if (!(timerShot >= m_Weapon[m_CurrentWP].speedRate))
				{
					return;
				}
				Debug.DrawLine(m_camera.position - Vector3.up * 0.06f, m_camera.forward, Color.red, 1E+10f);
				Ray ray = new Ray(m_camera.position - Vector3.up * 0.06f, m_camera.forward);
				m_Weapon[m_CurrentWP].boletsСurent--;
				audios.clip = m_Weapon[m_CurrentWP].shotSound;
				audios.Play();
				ShotFX[m_CurrentWP - 1].Play();
				timerShot = 0f;
				RaycastHit hitInfo;
				if (!Physics.Raycast(ray, out hitInfo, 100f))
				{
					return;
				}
				if (hitInfo.transform.tag != "Item" && hitInfo.transform.tag != "Car" && hitInfo.transform.tag != "Player")
				{
					m_impacts[m_currentImpact].transform.position = hitInfo.point + hitInfo.normal * 0.01f;
					m_impacts[m_currentImpact].transform.rotation = Quaternion.LookRotation(-hitInfo.normal);
                    m_impacts[m_currentImpact].transform.SetParent(hitInfo.transform);
                    if (!m_impacts[m_currentImpact].activeSelf)
					{
						m_impacts[m_currentImpact].SetActive(true);
					}
                    m_impacts[m_currentImpact].GetComponent<Impact>().fx.Play();
                    if (++m_currentImpact >= m_maxImpacts)
					{
						m_currentImpact = 0;
					}
				}
				if (hitInfo.transform.tag == "Player" && hitInfo.transform.gameObject != base.transform.gameObject)
				{
					PlayerHealth component = hitInfo.transform.GetComponent<PlayerHealth>();
					if (component.m_currentHP > 0f)
					{
						if (PhotonNetwork.room != null && !PhotonNetwork.offlineMode)
						{
							if (hitInfo.transform.tag == "Player")
							{
								component.photonView.RPC("TakeDamage", PhotonTargets.All, m_Weapon[m_CurrentWP].Damage, hitInfo.point);
							}
						}
						else if (hitInfo.transform.tag == "Player")
						{
							component.TakeDamage(m_Weapon[m_CurrentWP].Damage, hitInfo.point);
						}
					}
				}
				ReloadWhenChangeWeapon();
			}
		}
		else
		{
			audios.clip = m_Weapon[m_CurrentWP].shotSound;
			audios.Play();
		}
	}

	public void ReloadWhenChangeWeapon()
	{
		if (!(anim == null))
		{
			anim.SetBool("Shoot_b", false);
			if (m_Weapon[m_CurrentWP].boletsСurent <= 0)
			{
				anim.SetBool("Reload_b", true);
			}
			else if (m_Weapon[m_CurrentWP].boletsСurent > 0)
			{
				anim.SetBool("Reload_b", false);
			}
		}
	}

	public void ReloadingWeapon()
	{
		m_Weapon[m_CurrentWP].boletsСurent = m_Weapon[m_CurrentWP].boletsMax;
		anim.SetBool("Reload_b", false);
	}
}
